<?php

declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/10/31
 * Time: 5:22 PM
 */

namespace App\Entity;

use App\Models\Product;
use App\Utils\ImageUtil;
use App\Utils\UserLocationSessionUtil;

class ProductEntity extends ABaseEntity
{
    private $productCityOpen;
    private $productCityModel;

    public function __construct(Product $model)
    {
        $cityId = UserLocationSessionUtil::getLocationCityId();

        $productId   = $model->id;
        $productOpen = Product::where('product_pid', $productId)
            ->where('product_city', $cityId)
            ->where('product_open_city_config', 1)  // 是否启用城市单独配置
            ->first();

        if (!empty($productOpen)) {
            $this->productCityOpen = $productOpen;
        }

        $this->productCityModel = $productOpen;

        $this->model = $model;
    }

    public function setId(int $id): void
    {
        $this->model->id = $id;
    }

    /**
     * @return mixed
     */
    public function getProductCityModel()
    {
        return $this->productCityModel;
    }

    /**
     * @param mixed $productCityModel
     */
    public function setProductCityModel($productCityModel): void
    {
        $this->productCityModel = $productCityModel;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->model->id;
    }

    /**
     * @return string
     */
    public function getBType()
    {
        return $this->model->btype;
    }

    public function getTitle()
    {
        return $this->model->title;
    }

    /**
     * @return string
     */
    public function getImage()
    {
        if ($this->productCityOpen) {
            return ImageUtil::groupImageUrl($this->productCityOpen->image);
        } else {
            return ImageUtil::groupImageUrl($this->model->image);
        }
    }

    /**
     * @return string
     */
    public function getOrderImage()
    {
        if ($this->productCityOpen) {
            return ImageUtil::groupImageUrl($this->productCityOpen->order_image);
        } else {
            return ImageUtil::groupImageUrl($this->model->order_image);
        }
    }

    public function getProductMenuImage()
    {
        if ($this->productCityOpen) {
            return ImageUtil::groupImageUrl($this->productCityOpen->product_menu_image);
        } else {
            return ImageUtil::groupImageUrl($this->model->product_menu_image);
        }
    }

    public function getProductDesc()
    {
        if ($this->productCityOpen) {
            return $this->productCityOpen->product_desc;
        } else {
            return $this->model->product_desc;
        }
    }

    public function getDescription()
    {
        if ($this->productCityOpen) {
            return $this->productCityOpen->description;
        } else {
            return $this->model->description;
        }
    }

    /**
     * @return int
     */
    public function getOnSale()
    {
        return $this->model->on_sale;
    }

    public function getPrice()
    {
        return $this->model->price ?: '0';
    }

    public function getOldPrice()
    {
        return $this->model->old_price ?: '0';
    }

    public function getProductShowType()
    {
        if ($this->productCityOpen) {
//            if(!$this->productCityOpen->product_show_type){
//                Log::info("当前产品id:{$this->productCityOpen->id} product_show_type 必须有值");
//            }
            return $this->productCityOpen->product_show_type;
        } else {
//            if(!$this->model->product_show_type){
//                Log::info("当前产品id:{$this->model->id} product_show_type 必须有值");
//            }
            return $this->model->product_show_type;
        }
    }

    /**
     * @return int
     */
    public function getProductCity()
    {
        if ($this->productCityOpen) {
            return $this->productCityOpen->product_city;
        } else {
            return $this->model->product_city;
        }
    }

    /**
     * @return int
     */
    public function getProductBuyNumLeftDesc()
    {
        if ($this->productCityOpen) {
            return $this->productCityOpen->product_buy_num_left_desc;
        } else {
            return $this->model->product_buy_num_left_desc;
        }
    }

    public function getProductCategoryId()
    {
        return $this->model->product_category_id;
    }

    public function getProductOpenCityConfig()
    {
        if ($this->productCityOpen) {
            return $this->productCityOpen->product_open_city_config;
        } else {
            return $this->model->product_open_city_config;
        }
    }

    public function getCityProductOpenCityConfig()
    {
        if ($this->productCityModel) {
            return $this->productCityModel->product_open_city_config;
        } else {
            return 0;
        }
    }

    public function getCityProductShowType()
    {
        if ($this->productCityModel) {
            return $this->productCityModel->product_show_type;
        } else {
            return 0;
        }
    }

    /**
     * @return string
     */
    public function getCityImage()
    {
        if ($this->productCityModel) {
            return ImageUtil::groupImageUrl($this->productCityModel->image);
        } else {
            return '';
        }
    }

    /**
     * @return string
     */
    public function getCityOrderImage()
    {
        if ($this->productCityModel) {
            return returnImage($this->productCityModel->order_image);
        } else {
            return '';
        }
    }

    public function getCityProductMenuImage()
    {
        if ($this->productCityModel) {
            return ImageUtil::groupImageUrl($this->productCityModel->product_menu_image);
        } else {
            return '';
        }
    }

    public function getCityProductDesc()
    {
        if ($this->productCityModel) {
            return $this->productCityModel->product_desc;
        } else {
            return '';
        }
    }

    public function getCityDescription()
    {
        if ($this->productCityModel) {
            return $this->productCityModel->description;
        } else {
            return '';
        }
    }

    public function getCityProductBuyNumLeftDesc()
    {
        if ($this->productCityModel) {
            return $this->productCityModel->product_buy_num_left_desc;
        } else {
            return '';
        }
    }

    /**
     * @return string
     */
    public function getSpecification()
    {
        return $this->productCityModel['specification'];
    }
}
